/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef INTEROP_TYPES_H_
#define INTEROP_TYPES_H_

#include <cstdint>

// NOLINTBEGIN

typedef enum InteropTag {
    INTEROP_TAG_UNDEFINED = 101,
    INTEROP_TAG_INT32 = 102,
    INTEROP_TAG_FLOAT32 = 103,
    INTEROP_TAG_STRING = 104,
    INTEROP_TAG_LENGTH = 105,
    INTEROP_TAG_RESOURCE = 106,
    INTEROP_TAG_OBJECT = 107,
} InteropTag;

typedef enum InteropRuntimeType {
    INTEROP_RUNTIME_UNEXPECTED = -1,
    INTEROP_RUNTIME_NUMBER = 1,
    INTEROP_RUNTIME_STRING = 2,
    INTEROP_RUNTIME_OBJECT = 3,
    INTEROP_RUNTIME_BOOLEAN = 4,
    INTEROP_RUNTIME_UNDEFINED = 5,
    INTEROP_RUNTIME_BIGINT = 6,
    INTEROP_RUNTIME_FUNCTION = 7,
    INTEROP_RUNTIME_SYMBOL = 8,
    INTEROP_RUNTIME_MATERIALIZED = 9,
} InteropRuntimeType;

typedef float InteropFloat32;
typedef double InteropFloat64;
typedef int32_t InteropInt32;
typedef unsigned int InteropUInt32;
typedef int64_t InteropInt64;
typedef int8_t InteropInt8;
typedef uint8_t InteropUInt8;
typedef int64_t InteropDate;
typedef int8_t InteropBoolean;
typedef const char *InteropCharPtr;
typedef void *InteropNativePointer;

struct InteropVMContextRaw;
typedef struct InteropVMContextRaw *InteropVMContext;
struct InteropPipelineContextRaw;
typedef struct InteropPipelineContextRaw *InteropPipelineContext;
struct InteropVMObjectRaw;
typedef struct InteropVMObjectRaw *InteropVMObject;
struct InteropNode;
typedef struct InteropNode *InteropNodeHandle;
typedef struct InteropDeferred {
    void *handler;
    void *context;
    void (*resolve)(struct InteropDeferred *thiz, uint8_t *data, int32_t length);
    void (*reject)(struct InteropDeferred *thiz, const char *message);
} InteropDeferred;

// Binary layout of InteropString must match that of KStringPtrImpl.
typedef struct InteropString {
    const char *chars;
    InteropInt32 length;
} InteropString;

typedef struct InteropEmpty {
    InteropInt32 dummy;  // Empty structs are forbidden in C.
} InteropEmpty;

typedef struct InteropNumber {
    InteropInt8 tag;
    union {
        InteropFloat32 f32;
        InteropInt32 i32;
    };
} InteropNumber;

// Binary layout of InteropLength must match that of KLength.
typedef struct InteropLength {
    InteropInt8 type;
    InteropFloat32 value;
    InteropInt32 unit;
    InteropInt32 resource;
} InteropLength;

typedef struct InteropCustomObject {
    char kind[20];
    InteropInt32 id;
    // Data of custom object.
    union {
        InteropInt32 ints[4];
        InteropFloat32 floats[4];
        void *pointers[4];
        InteropString string;
    };
} InteropCustomObject;

typedef struct InteropUndefined {
    InteropInt32 dummy;  // Empty structs are forbidden in C.
} InteropUndefined;

typedef struct InteropVoid {
    InteropInt32 dummy;  // Empty structs are forbidden in C.
} InteropVoid;

typedef struct InteropFunction {
    InteropInt32 id;
} InteropFunction;
typedef InteropFunction InteropCallback;
typedef InteropFunction InteropErrorCallback;

typedef struct InteropMaterialized {
    InteropNativePointer ptr;
} InteropMaterialized;

typedef struct InteropCallbackResource {
    InteropInt32 resourceId;
    void (*hold)(InteropInt32 resourceId);
    void (*release)(InteropInt32 resourceId);
} InteropCallbackResource;

typedef struct InteropBuffer {
    InteropCallbackResource resource;
    InteropNativePointer data;
    InteropInt64 length;
} InteropBuffer;

// NOLINTEND

#endif  // INTEROP_TYPES_H_
